package com.ruyuan.user.server.domain.converter;

import com.ruyuan.common.model.Features;
import com.ruyuan.user.api.model.enums.UserJobRelationTypeEnum;
import com.ruyuan.user.server.domain.model.Job;
import com.ruyuan.user.server.domain.service.request.SaveUserMultiJobReq;
import com.ruyuan.user.server.repository.bean.JobDO;
import com.ruyuan.user.server.repository.bean.UserJobRelationDO;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xx
 */
@Component
public class JobConverter {

    public Job convertDOToModel(JobDO jobDO) {
        if (jobDO == null) {
            return null;
        }
        Job job = new Job();
        BeanUtils.copyProperties(jobDO, job);
        if (StringUtils.isNotBlank(jobDO.getFeatures())) {
            job.setFeatures(Features.of(jobDO.getFeatures()));
        }
        job.setIsDeleted("y".equals(jobDO.getIsDeleted()));
        return job;
    }

    public JobDO convertModelToDO(Job job) {
        if (job == null) {
            return null;
        }
        JobDO jobDO = new JobDO();
        BeanUtils.copyProperties(job, jobDO);
        if (!job.getFeatures().isEmpty()) {
            jobDO.setFeatures(job.getFeatures().toString());
        }
        jobDO.setIsDeleted(job.getIsDeleted() == null || !job.getIsDeleted() ? "n" : "y");
        if (job.getId() == null) {
            jobDO.setCreator(job.getModifier());
            jobDO.setGmtCreate(new Date());
        }
        jobDO.setGmtModified(new Date());
        return jobDO;
    }

    public List<UserJobRelationDO> convertToUserJobRelationDO(SaveUserMultiJobReq req) {
        Map<UserJobRelationTypeEnum, List<Job>> userMultiJob = req.getUserMultiJob();
        if (MapUtils.isNotEmpty(userMultiJob)) {
            return userMultiJob.keySet().stream().filter(relation -> relation != UserJobRelationTypeEnum.MAIN_JOB)
                    .map(relation -> {
                        List<Job> otherJobs = userMultiJob.get(relation);
                        return otherJobs.stream().map(job -> {
                            UserJobRelationDO userJobRelationDO = new UserJobRelationDO();
                            userJobRelationDO.setJobId(job.getId());
                            userJobRelationDO.setUserId(req.getUserId());
                            userJobRelationDO.setRelationType(relation.getCode());
                            userJobRelationDO.setCreator(req.getOperator());
                            userJobRelationDO.setModifier(req.getOperator());
                            userJobRelationDO.setIsDeleted("n");
                            userJobRelationDO.setGmtModified(new Date());
                            userJobRelationDO.setGmtCreate(new Date());
                            return userJobRelationDO;
                        }).collect(Collectors.toList());
                    }).flatMap(Collection::stream).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

}
